<?php
namespace collecter\controllers;

use collecter\data\tasks\Hktv8;
use Yii;
use yii\web\Controller;

/**
 * Site controller
 */
class TestController extends Controller{


    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex(){
        $db = Yii::$app->db;

       // $sql = "INSERT INTO recite_knowledge(c1, c2, c3, cid, question, answer, last_right_time)  VALUES(2, 0, 0, 2, 'sdf', 'dfg', now())";
        $sql = "INSERT INTO recite_knowledge(c1, c2, c3, cid, question, answer)  VALUES(2, 0, 0, 2, 'sdf', 'dfg')";
        $command = $db->createCommand($sql);
         //echo $command->getRawSql();exit;
       $line = $command->execute();
        var_dump($line);

        $id = $db->getLastInsertID();
        var_dump($id);
    }

    public function actionTest(){

        $obj = new Hktv8();

        $url = "https://www.hktv8.com/thread-66391-1-1.html";
        $url = "https://www.hktv8.com/thread-36826-1-1.html";
        $result = $obj->getTask($url);


        header('Content-Type: text/html; charset=utf-8');

        $cookie_file = dirname(__FILE__).'/cookie.txt';
        $cookie_file = 'd:/cookie.txt';
//$cookie_file = tempnam("tmp","cookie");

//先获取cookies并保存

        $ch = curl_init($url); //初始化
        curl_setopt($ch, CURLOPT_HEADER, 0); //不返回header部分
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串，而非直接输出
        curl_setopt($ch, CURLOPT_COOKIEJAR,  $cookie_file); //存储cookies
        curl_exec($ch);
        curl_close($ch);

//使用上面保存的cookies再次访问
        $url = "https://www.hktv8.com/thread-36826-1-1.html";
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); //使用上面获取的cookies
        $response = curl_exec($ch);
        curl_close($ch);

        echo $response;

        //print_r($result);
        exit;
    }

}
